<?php

namespace Api\Model;

use Think\Model;

class PostModel extends Model {

    function addPost($class_id, $user_id, $post_excerpt, $post_content, $media, $is_vote, $is_comment = 1) {
        $data = array(
            'class_id' => $class_id,
            'user_id' => $user_id,
            'post_excerpt' => $post_excerpt,
            'post_content' => $post_content,
            'media' => $media,
            'is_vote' => $is_vote,
            'is_comment' => $is_comment,
            'post_content' => $post_content,
            'createtime' => date('Y-m-d H:i;s'),
        );
        return $this->add($data);
    }

    function addPosts($cids, $user_id, $post_excerpt, $post_content, $media, $is_vote, $is_comment = 1) {
        foreach ($cids as $value) {
            $dataList[] = array(
                'class_id' => $value,
                'user_id' => $user_id,
                'post_excerpt' => $post_excerpt,
                'post_content' => $post_content,
                'media' => $media,
                'is_vote' => $is_vote,
                'is_comment' => $is_comment,
                'post_content' => $post_content,
                'createtime' => date('Y-m-d H:i;s'),
            );
        }
        return $this->addAll($dataList);
    }

    function getlist($user_id, $class_id = 0, $start = 0, $limit = 20) {
        $where = 'jl_classmember.user_id=' . $user_id . ' AND (jl_post.is_hide=0 OR (jl_post.is_hide=1 AND jl_classmember.power >=jl_post.showid))';
        if ($class_id) {
            $where = 'jl_classmember.user_id=' . $user_id . 'AND jl_classmember.class_id =' . $class_id . ' AND (jl_post.is_hide=0 OR (jl_post.is_hide=1 AND jl_classmember.power >=jl_post.showid))';
        }
        $field = 'jl_post.id as post_id';
        $join1 = '__CLASSMEMBER__ on __POST__.class_id=__CLASSMEMBER__.class_id AND ';
        $join2 = '__ICONS__ on __POST__.id=__ICONS__.post_id';
        $join3 = '__CLASSMEMBER__ on __POST__.class_id=__CLASSMEMBER__.class_id';
        return $this->where($where)->join($join1)->join($join2)->field($field)->limit($limit)->select();
    }

    function getPostbypid($post_id) {
        $where = array('jl_post.id' => $post_id);
        $join = array('LEFT JOIN __CLASSMEMBER__ ON  __POST__.user_id=__CLASSMEMBER__.user_id', 'LEFT JOIN __CLASS__ ON __POST__.class_id= __CLASS__.id');
        $field = 'jl_post.id as id,jl_post.class_id as class_id,jl_post.user_id as user_id,jl_post.post_content as content,jl_post.media as media,jl_post.is_comment as is_comment,jl_post.is_vote as is_vote,jl_classmember.rename as username,jl_class.classname as classname,jl_post.updatetime as createtime';
        return $this->where($where)->join($join)->field($field)->find();
    }

    function getPostlistbycids($cids, $start = 0, $limit = 20) {
        $map['jl_post.class_id'] = array('in', $cids);
        $limit = $start . ',' . $limit;
        $field = 'jl_post.id as id,jl_post.class_id as class_id,jl_post.user_id as user_id,jl_post.post_excerpt as excerpt'
                . ',jl_post.media as media,jl_post.is_comment as is_comment,jl_post.is_vote as is_vote,'
                . 'jl_post.updatetime as createtime,jl_class.classname as classname,jl_classmember.rename as username';
        $join = array('LEFT JOIN __CLASS__ ON __POST__.class_id= __CLASS__.id', 'LEFT JOIN __CLASSMEMBER__ ON  __POST__.user_id=__CLASSMEMBER__.user_id and __POST__.class_id=__CLASSMEMBER__.class_id');
        return $this->distinct(TRUE)->where($map)->join($join)->field($field)->order('id desc')->limit($limit)->select();
    }

    function getPostlistbycid($cid, $start = 0, $limit = 20) {
        $where = array('jl_post.class_id' => $cid);
        $limit = $start . ',' . $limit;
        $field = 'jl_post.id as id,jl_post.class_id as class_id,jl_post.user_id as user_id,jl_post.post_excerpt as excerpt'
                . ',jl_post.media as media,jl_post.is_comment as is_comment,jl_post.is_vote as is_vote,'
                . 'jl_post.updatetime as createtime,jl_class.classname as classname,jl_classmember.rename as username';
        $join = array('LEFT JOIN __CLASS__ ON __POST__.class_id= __CLASS__.id', 'LEFT JOIN __CLASSMEMBER__ ON  __POST__.user_id=__CLASSMEMBER__.user_id and __POST__.class_id=__CLASSMEMBER__.class_id');
        return $this->distinct(TRUE)->where($where)->field($field)->join($join)->order('id desc')->limit($limit)->select();
    }

    function getPostnumbycid($cid) {
        $where = array('class_id' => $cid);
        return $this->where($where)->count('id');
    }

}
